import os
import re

def findCoupon(basepath):
    for root, dir, files in os.walk(basepath):
        for file in files:
            with open(fr'{root}\{file}', "r", encoding='utf-8') as f:
                line = f.read()
                if "<WN:Coupon>" in line:
                    lRetailStoreID = re.findall(r'<RetailStoreID>(.*)</RetailStoreID>', line)[0]
                    lWorkstationNmbr = re.findall(r'<WorkstationID>(.*)</WorkstationID>', line)[0]
                    SequenceNumber = re.findall(r'<SequenceNumber>(.*)</SequenceNumber><BusinessDayDate>', line)[0]
                    BeginDateTime = re.findall(r'<BeginDateTime>(.*)</BeginDateTime>', line)[0]
                    ScanCode = re.findall(r'<ScanCode>(.*)</ScanCode>', line)[0]
                    with open("ID.txt", "a", encoding='utf-8') as f1:
                        year = BeginDateTime[0:4]
                        month = str(BeginDateTime[5:7]).rjust(2,'0')
                        day = str(BeginDateTime[8:10].rjust(2,'0'))
                        h = str(BeginDateTime[11:13].rjust(2,'0'))
                        min = str(BeginDateTime[14:16].rjust(2,'0'))
                        t = str(BeginDateTime[17:19].rjust(2,'0'))
                        time = year + month + day + h + min + t
                        print('{"couponCode":"'+ ScanCode + '","orderCode:"'+ lRetailStoreID + lWorkstationNmbr + SequenceNumber + time + '"}' + "\n")
                        f1.write('{"couponCode":"'+ ScanCode + '","orderCode:"'+ lRetailStoreID + lWorkstationNmbr + SequenceNumber + time + '"}' + "\n")
                    f1.close()
            f.close()


if __name__ == '__main__':
    basepath = r'D:\Disktop\新建文件夹 (11)'
    # basepath = input(r'')
    findCoupon(basepath)
